#!/usr/bin/ruby -w

require "pp"
require "digest/sha2"

begin
  WORD = /\S+/
  PASSWORD = /\S+/
  NUMBER = /\d+/
  EMAIL = /@/  

  data = {
	:Username    => [WORD, ""],
	:Artistname => [WORD, ""],
  	:Phone      => [NUMBER, ""],
  	:Email       => [EMAIL, ""],
  	:Password    => [PASSWORD, ""],
  }
 
  data.each_key do |key|
    correct = false
    puts "please enter %s:" % key
    
    until correct
      input = STDIN.readline.strip
      if(input =~ data[key][0])
        data[key][1] = input 
        correct = true
      else
        puts "ERROR: input should match " + data[key][0].to_s
      end
    end
  end

  data[:Admin] = ["", 2]

  data[:salt] = ["", (Digest::SHA2.new << Time.now.to_s + data.values.join(", ") + rand(1024).to_s).to_s]
  data[:passwordHash] = ["", (Digest::SHA2.new << data[:salt][1] + data[:Password][1]).to_s]

  sql = "INSERT INTO workers(name, created_at, updated_at, artist_name, mobile, email, password, salt, admin) VALUES('%s', %d, %d, '%s', '%s', '%s', '%s', '%s', %d);" % 
	[
	  data[:Username][1],
          Time.now.to_i,
          Time.now.to_i,
          data[:Artistname][1],
          data[:Phone][1],
          data[:Email][1],
          data[:passwordHash][1],
          data[:salt][1],
          data[:Admin][1]
        ]

   puts sql
          
end
